package com.jkc.xh.manager;

import android.widget.Toast;

import com.jkc.xh.MainApplication;
import com.jkc.xh.constants.Constant;
import com.jkc.xh.models.MessageModel;
import com.jkc.xh.models.UserInfo;
import com.jkc.xh.network.ApiClient;
import com.jkc.xh.network.RestCallBack;
import com.jkc.xh.network.models.ConfigResponse;
import com.jkc.xh.network.models.GetSmsCodeRequest;
import com.jkc.xh.network.models.GetSmsListResponse;
import com.jkc.xh.network.models.InviteFriendResponse;
import com.jkc.xh.network.models.LoginRequest;
import com.jkc.xh.network.models.LoginResponse;
import com.jkc.xh.network.models.MessageResponse;
import com.jkc.xh.network.models.ModifyPwdRequest;
import com.jkc.xh.network.models.RegisterRequest;
import com.jkc.xh.network.models.ResponseEntity;
import com.jkc.xh.network.models.RestError;
import com.jkc.xh.network.models.ServerListResponse;
import com.jkc.xh.network.models.SoftVersionResponse;
import com.jkc.xh.network.models.SystemHintResponse;
import com.jkc.xh.utils.EventBusUtils;
import com.jkc.xh.utils.SecurePreferences;
import com.jkc.xh.utils.Tool;

import retrofit.Call;

/**
 * Created by wangjie28 on 2015/12/1.
 */
public class UserManager {

    //    private static UserInfo userInfo;
    private static final String PREFERENCE_USER_NAME = "preference_user_name";

    private static final String PREFERENCE_KEY_UID = "preference_key_uid";

    private static final String PREFERENCE_KEY_SID = "preference_key_sid";

    private static final String PREFERENCE_KEY_MOBILE = "preference_key_moible";

    public synchronized static boolean isLogin() {
        return SecurePreferences.getInstance().contains(PREFERENCE_KEY_UID) && SecurePreferences.getInstance().contains(PREFERENCE_KEY_SID);
    }

    public synchronized static String getUid() {
        return SecurePreferences.getInstance().getString(PREFERENCE_KEY_UID, null);
    }

    public synchronized static String getSid() {
        return SecurePreferences.getInstance().getString(PREFERENCE_KEY_SID, null);
    }

    public synchronized static void clearLoginInfo() {
        SecurePreferences.getInstance().edit().remove(PREFERENCE_KEY_UID).remove(PREFERENCE_KEY_SID).commit();
    }

    public static String getLastLoginName() {
        return SecurePreferences.getInstance().getString(PREFERENCE_USER_NAME, null);
    }

    public static String getMobile() {
        return SecurePreferences.getInstance().getString(PREFERENCE_KEY_MOBILE, null);
    }

    public static Call getUserInfo(RestCallBack<UserInfo> callBack) {
        if (getUid() == null) {
            return null;
        }
        Call<ResponseEntity<UserInfo>> call = ApiClient.getApiService().userInfo(getUid());
        call.enqueue(new RestCallBack<UserInfo>() {
            @Override
            public void success(UserInfo info) {
                SecurePreferences.getInstance().edit().putString(PREFERENCE_KEY_MOBILE, info.mobile).commit();
                callBack.success(info);
            }

            @Override
            public void failure(RestError error) {
                callBack.failure(error);
            }
        });
        return call;
    }

    /**
     * 修改用户信息
     *
     * @param info
     * @param callBack
     * @return
     */
    public static Call updateUserInfo(UserInfo info, RestCallBack<Object> callBack) {
        if (getUid() == null) {
            return null;
        }
        Call<ResponseEntity<Object>> call = ApiClient.getApiService().updateUserInfo(getUid(), info);
        call.enqueue(callBack);
        return call;
    }

    /**
     * 修改用户密码
     *
     * @param callBack
     * @return
     */
    public static Call modifyUserPwd(String oldpwd, String newpwd, RestCallBack<Object> callBack) {
        if (getUid() == null) {
            return null;
        }
        Call<ResponseEntity<Object>> call = ApiClient.getApiService().updateUserPwd(getUid(), new ModifyPwdRequest(oldpwd, newpwd));
        call.enqueue(callBack);
        return call;
    }

    public static void getUserInfoByEvent() {
        if (getUid() == null) {
            return;
        }
        Call<ResponseEntity<UserInfo>> call = ApiClient.getApiService().userInfo(getUid());
        call.enqueue(new RestCallBack<UserInfo>() {
            @Override
            public void success(UserInfo userInfo) {
                EventBusUtils.post(userInfo);
            }

            @Override
            public void failure(RestError error) {
                Toast.makeText(MainApplication.getInstance(), error.msg, Toast.LENGTH_SHORT).show();
            }
        });
    }


    public static Call login(final String username, String password, final RestCallBack<LoginResponse> callBack) {
        final Call<ResponseEntity<LoginResponse>> call = ApiClient.getApiService().login(new LoginRequest(username, password));
        call.enqueue(new RestCallBack<LoginResponse>() {
            @Override
            public void success(LoginResponse loginResponse) {
                SecurePreferences.getInstance().edit().putString(PREFERENCE_USER_NAME, username).commit();
                SecurePreferences.getInstance().edit().putString(PREFERENCE_KEY_UID, loginResponse.uid).putString(PREFERENCE_KEY_SID, loginResponse.sid).commit();
//                Toast.makeText(MainApplication.getInstance(), "登录成功", Toast.LENGTH_SHORT).show();
                EventBusUtils.post(Constant.ACTION_LOGIN);
                if (callBack != null) {
                    callBack.success(loginResponse);
                }
            }

            @Override
            public void failure(RestError error) {
                Toast.makeText(MainApplication.getInstance(), error.msg, Toast.LENGTH_SHORT).show();
                if (callBack != null) {
                    callBack.failure(error);
                }
            }
        });
        return call;
    }

    public static void getServersList(final RestCallBack<ServerListResponse> callBack) {
        final Call<ResponseEntity<ServerListResponse>> call = ApiClient.getApiService().getServersList();
        call.enqueue(new RestCallBack<ServerListResponse>() {
            @Override
            public void success(ServerListResponse serverListResponse) {
                if (callBack != null) {
                    callBack.success(serverListResponse);
                }
            }

            @Override
            public void failure(RestError error) {
                Toast.makeText(MainApplication.getInstance(), error.msg, Toast.LENGTH_SHORT).show();
                if (callBack != null) {
                    callBack.failure(error);
                }
            }
        });
    }

    /*1302 短信通道*/
    public static Call getSmsSvrList(final RestCallBack<GetSmsListResponse> callBack) {
        final Call<ResponseEntity<GetSmsListResponse>> call = ApiClient.getApiService().getSmsSvrList();
        call.enqueue(callBack);
        return call;
    }

    /*1300 发送短信验证码*/
    public static void getSmsCode(String mobile, int mod, long smsid, final RestCallBack<Object> callBack) {
        final Call<ResponseEntity<Object>> call = ApiClient.getApiService().getSmsCode(new GetSmsCodeRequest(mobile, mod, smsid));
        call.enqueue(callBack);
    }

    /*1200 手机注册*/
    public static void register(RegisterRequest info, final RestCallBack<LoginResponse> callBack) {
        final Call<ResponseEntity<LoginResponse>> call = ApiClient.getApiService().register(info);
        call.enqueue(callBack);
    }

    /**
     * 获取最新版本
     *
     * @param callBack
     */
    public static Call getLastSoftVersion(final RestCallBack<SoftVersionResponse> callBack) {
        final Call<ResponseEntity<SoftVersionResponse>> call = ApiClient.getApiService().getLastSoftVersion(Tool.getAppVersion(MainApplication.getInstance(), true));
        call.enqueue(callBack);
        return call;
    }

    /**
     * 3003 邀请好友
     */
    public static Call doInviteFriend(final RestCallBack<InviteFriendResponse> callBack) {
        final Call<ResponseEntity<InviteFriendResponse>> call = ApiClient.getApiService().doInviteFriend();
        call.enqueue(callBack);
        return call;
    }

    /**
     * 我的消息列表
     *
     * @param pageIndex
     * @param callBack
     * @return
     */
    public static Call getUserCenterMessage(int pageIndex, final RestCallBack<MessageResponse> callBack) {
        final Call<ResponseEntity<MessageResponse>> call = ApiClient.getApiService().getUserCenterMessage(pageIndex);
        call.enqueue(callBack);
        return call;
    }

    public static Call getUserCenterMessageDetail(String id,final RestCallBack<MessageModel> callBack){
        final Call<ResponseEntity<MessageModel>> call = ApiClient.getApiService().getUserCenterMessageDetail(id);
        call.enqueue(callBack);
        return call;
    }

    public static Call getCpmfConfig(final RestCallBack<ConfigResponse> callBack){
        final Call<ResponseEntity<ConfigResponse>> call = ApiClient.getApiService().getConfig();
        call.enqueue(callBack);
        return call;
    }
    public static Call getCpmfHintConfig(final RestCallBack<SystemHintResponse> callBack){
        final Call<ResponseEntity<SystemHintResponse>> call = ApiClient.getApiService().getSysteHintInfo();
        call.enqueue(callBack);
        return call;
    }

}
